package com.hqyj.springbootday03.modules.common.controller;

import com.hqyj.springbootday03.modules.common.vo.ImageType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * EnumController
 */
@RestController
@RequestMapping("/api")
public class EnumController {

	public static Map<String, Class> map = new HashMap<String, Class>(){{
		put("imageType", ImageType.class);
	}};
//	static {
//		map.put("imageType", ImageType.class);
//	}

	/**
	 * 127.0.0.1/api/dictionary/imageType ---- get
	 */
	@GetMapping(value = "/dictionary/{enumKey}")
	public List<Object> getEnum(@PathVariable String enumKey) {
		try {
			if (map.containsKey(enumKey)) {
				Class clazz = map.get(enumKey);
				Method method = clazz.getDeclaredMethod("values");
				Object[] temp = (Object[]) method.invoke(null);
				return Arrays.asList(temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
